package com.iuv.shortpromotion.controller;


import com.iuv.shortpromotion.common.CommonOperateCode;
import com.iuv.shortpromotion.domain.DemoSmsPromotionTbl;
import com.iuv.shortpromotion.service.DemoSmsPromotionService;
import com.iuv.shortpromotion.task.SendBatchSmsTask;
import com.iuv.shortpromotion.util.RespondUtil;
import com.iuv.shortpromotion.util.ShortPromotionUtil;
import com.iuv.shortpromotion.vo.AddSmsTemplateResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

/**
 *活动推广控制类
 * @author mic
 * @date 2020-11-12
 */
@RequestMapping("promotion")
@RestController
public class PromotionController {

    @Autowired
    private DemoSmsPromotionService demoSmsPromotionService;
    private static int count;

    /**
     *  创建 推广活动
     *
     * @param demoSmsPromotionTbl
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public Map<String, Object> save(DemoSmsPromotionTbl demoSmsPromotionTbl) {
        demoSmsPromotionService.insert(demoSmsPromotionTbl);
        return RespondUtil.respondSuccess(CommonOperateCode.SysCode.SAVE_SUCCES, CommonOperateCode.SysMsg.SAVE_SUCCES);
    }

    /**
     *  更新 推广活动
     * @param demoSmsPromotionTbl
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public Map<String, Object> update(DemoSmsPromotionTbl demoSmsPromotionTbl) {
        demoSmsPromotionService.update(demoSmsPromotionTbl);
        return RespondUtil.respondSuccess(CommonOperateCode.SysCode.UPDATE_SUCCES, CommonOperateCode.SysMsg.UPDATE_SUCCES);
    }

    /**
     * 删除 推广活动
     * @param promotionId
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Map<String, Object> delete(Integer promotionId) {
        /** TODO 活动推广 问题四 [while 特殊循环结构，不设置判断条件和递增条件]
         * while （true）
         * 死循环设计
         * 如果 i==5 解除本次循环
         * 如果i==10 结束循环
         **/
        int i = 0;
        while (true) {
            i++;
            if (i == 5){
                continue;
            }
            if (i == 10) {
                break;
            }
        }

        /**
         * END
         */
        demoSmsPromotionService.delete(promotionId);
        return RespondUtil.respondSuccess(CommonOperateCode.SysCode.DELETE_SUCCES, CommonOperateCode.SysMsg.DELETE_SUCCES);
    }
    /**
     *  搜索
     * @param demoSmsPromotionTbl
     * @param pageable
     * @return
     */
    @RequestMapping("listSearch")
    @ResponseBody
    public Map<String, Object> listSearch(DemoSmsPromotionTbl demoSmsPromotionTbl, Pageable pageable) {
        /** TODO  活动推广 问题一  [ 常用循环条件 固定条件]
         *  统计次数 调用 listSearch 循环5次，相当于点击5次
         */
        for (int i = 0; i < 5; i++) {
            count++;
        }

        /**
         * END
         */
        Map<String, Object> result =RespondUtil.respondSuccess("", "");
        Page<DemoSmsPromotionTbl> tbls = demoSmsPromotionService.listSearch(demoSmsPromotionTbl, pageable);
        /** TODO  活动推广 问题二  [ 迭代器模式循环 ]
         *  迭代器循环条件
         *  并输出id
         */
        for (DemoSmsPromotionTbl tbl : tbls) {
            System.out.println("当前活动id:" + tbl.getPromotionId());
        }
        /**
         * END
         */

        result.put("data", tbls);
        System.out.println("已经点击了" + count);
        return result;
    }

    /**
     * 发送  推广活动短信
     * @param promotionId
     * @return
     */
    @RequestMapping("pushPromotion")
    @ResponseBody
    public Map<String, Object> pushPromotion(Integer promotionId) {
        /** TODO 活动推广 问题三 [循环条件改变]
         * for循环，每次累加10
         * 如果 count条件 == promotionId ，结束本次循环
         * 如果 i > promotionId 结束循环
         **/
        for (int i = 0; i < 100; i += 10 ) {
            if (count == promotionId) {
                continue;
            }
            if (i > promotionId) {
                break;
            }
        }

        /**
         * END
         */
        String sysCode = CommonOperateCode.SysCode.SMS_SEND_WAIT;
        String sysMsg = CommonOperateCode.SysMsg.SMS_SEND_WAIT;
        DemoSmsPromotionTbl demoSmsPromotionTbl = demoSmsPromotionService.findByPromotionId(promotionId);
        if (demoSmsPromotionTbl != null) {
            boolean timeStatus = demoSmsPromotionService.checkPromotionEnd(demoSmsPromotionTbl);
            //活动时间已经结束
            if (timeStatus) {
                sysCode = CommonOperateCode.SysCode.PROMOTION_TIME_END;
                sysMsg = CommonOperateCode.SysMsg.PROMOTION_TIME_END;
            } else {
                if (StringUtils.trimToNull(demoSmsPromotionTbl.getTemplateCode()) == null) {
                    /**
                     * 模板未申请,申请模板
                     */
//                    AddSmsTemplateResponse addSmsTemplateResponse = ShortPromotionUtil.addSmsTemplate("2", demoSmsPromotionTbl.getPromotionName() + "模板", demoSmsPromotionTbl.getPushContents() +
//                            "回T退订", "应用于" + demoSmsPromotionTbl.getPromotionName() + "的推广");
                    AddSmsTemplateResponse addSmsTemplateResponse = ShortPromotionUtil.addSmsTemplate("2", demoSmsPromotionTbl.getPromotionName() + "模板", demoSmsPromotionTbl.getPushContents() +
                            "拒收请回复R", "应用于" + demoSmsPromotionTbl.getPromotionName() + "的推广");
                    if (addSmsTemplateResponse != null && "OK".equals(addSmsTemplateResponse.getCode())) {
                        /**
                         * 成功,加入处理任务
                         */
                        new SendBatchSmsTask(addSmsTemplateResponse.getTemplateCode(), demoSmsPromotionTbl, demoSmsPromotionService);
                    } else {
                        /**
                         * 失败
                         */
                         sysCode = CommonOperateCode.SysCode.SMS_SEND_ERROR;
                         sysMsg = CommonOperateCode.SysMsg.SMS_SEND_ERROR;
                        demoSmsPromotionTbl.setPushStatus(0);
                        demoSmsPromotionTbl.setErrorMessage(addSmsTemplateResponse.getMessage());
                        demoSmsPromotionService.save(demoSmsPromotionTbl);
                    }
                } else {
                    /**
                     * 模板已经申请成功，直接群发推广短信
                     */
                    demoSmsPromotionService.sendBatchSms(demoSmsPromotionTbl);
                }
            }
        }
        Map<String, Object> result = RespondUtil.respondSuccess(sysCode, sysMsg);
        return result;
    }

}
